import numpy as np
import cv2


def cover(img_w, img_c):
    # img_w:弹幕 , img_c:封面 .
    height, width = img_w.shape[:2]
    size = (width, height)
    img_c = cv2.resize(img_c, (size[0], size[1]))
    rows, cols, channels = img_w.shape
    rows, cols, channels = img_w.shape
    roi = img_w[0:rows, 0:cols]
    GrayImage = cv2.cvtColor(img_w, cv2.COLOR_BGR2GRAY)
    # cv2.imshow('GrayImage', GrayImage)
    # cv2.waitKey(0)
    # mask_bin 是黑白掩膜
    ret, mask_bin = cv2.threshold(GrayImage, 127, 255, cv2.THRESH_BINARY | cv2.THRESH_TRIANGLE)
    # mask_inv 是反色黑白掩膜
    mask_inv = cv2.bitwise_not(mask_bin)
    # cv2.imshow('GrayImage', mask_bin)
    # cv2.waitKey(0)
    # 黑白掩膜 和 大图切割区域 取和
    img1_bg = cv2.bitwise_and(roi, roi, mask=mask_bin)
    # 反色黑白掩膜 和 logo 取和
    img2_fg = cv2.bitwise_and(img_c, img_c, mask=mask_inv)
    dst = cv2.add(img1_bg, img2_fg)
    img_w[0:rows, 0:cols] = dst
    # cv2.imshow('GrayImage', img1_bg)
    # cv2.waitKey(0)
    for i in range(rows):
        for j in range(cols):
            if mask_inv[i, j] == 0:
                img2_fg[i, j] = (255, 255, 255)  # 此处替换背景颜色为白色，为BGR通道
    # cv2.imshow('GrayImage', img2_fg)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    return img2_fg